package com.shijie.sjoj.service.core;

import com.shijie.sjoj.model.api.user.UserService;
import com.shijie.sjoj.model.component.CryptoModel;
import com.shijie.sjoj.model.component.IdGeneratorRedis;
import com.shijie.sjoj.model.component.validate.IncludeValidate;
import com.shijie.sjoj.model.component.validate.NumberValidate;
import com.shijie.sjoj.model.component.validate.RegexValidate;
import com.shijie.sjoj.model.exception.UserException;
import com.shijie.sjoj.model.pojo.base.Gender;
import com.shijie.sjoj.model.pojo.base.Role;
import com.shijie.sjoj.model.pojo.entity.User;
import com.shijie.sjoj.model.utils.StringUtils;
import com.shijie.sjoj.service.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 用户服务接口实现类
 *
 * @author ZSJ
 */
@Slf4j
@DubboService
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final IdGeneratorRedis idGenerator;

    @Override
    public User getByUsername(String username) {
        // 无效值校验
        if (StringUtils.isBlank(username)) {
            throw UserException.throwError("无效的用户名");
        }
        // 获取用户
        return userMapper.getByUsername(username);
    }

    @Override
    public User getByUserId(Long userId) {
        // 无效值校验
        if (NumberValidate.invalidSys(userId)) {
            throw UserException.throwError("无效的用户id");
        }
        // 获取用户
        return userMapper.getByUserId(userId);
    }

    @Override
    public boolean save(User user) {
        // 无效值校验
        if (null == user) {
            throw UserException.throwError("无效的用户");
        }
        if (StringUtils.isBlank(user.getUsername())) {
            throw UserException.throwError("无效的用户名");
        }
        if (RegexValidate.invalidSysAccount(user.getAccount())) {
            throw UserException.throwError("无效的账号");
        }
        if (RegexValidate.invalidSysPassword(user.getPassword())) {
            throw UserException.throwError("无效的密码");
        }
        if (RegexValidate.invalidDefPhone(user.getPhone())) {
            throw UserException.throwError("无效的手机号");
        }
        // 用户格式化
        user.setUserId(idGenerator.nextUserId());
        user.setPassword(CryptoModel.encryptPassword(user.getPassword()));
        user.setRole(null == user.getRole() ? Role.User : user.getRole());
        user.setGender(null == user.getGender() ? Gender.NA : user.getGender());
        user.setAvatar(StringUtils.isBlank(user.getAvatar()) ? null : user.getAvatar());
//        user.setBirthday(null == user.getBirthday() ? null : user.getBirthday());
        user.setCity(IncludeValidate.invalidCity(user.getCity()) ? null : user.getCity());
        user.setIntroduction(StringUtils.isBlank(user.getIntroduction()) ? null : user.getIntroduction());
        // 新增用户
        return userMapper.save(user);
    }

    @Override
    public boolean modify(User user) {
        // 无效值校验
        if (null == user) {
            throw UserException.throwError("无效的用户");
        }
        if (null == user.getUserId() || user.getUserId() < 4294967297L) {
            throw UserException.throwError("无效的用户id");
        }
        // 用户格式化
        user.setUsername(StringUtils.isBlank(user.getUsername()) ? null : user.getUsername());
        user.setAvatar(StringUtils.isBlank(user.getAvatar()) ? null : user.getAvatar());
//        user.setBirthday(null==user.getBirthday() ? null : user.getBirthday());
        user.setCity(IncludeValidate.invalidCity(user.getCity()) ? null : user.getCity());
        user.setIntroduction(StringUtils.isBlank(user.getIntroduction()) ? null : user.getIntroduction());
        // 更新用户信息
        return userMapper.modify(user);
    }


}
